;
(function () {
	new Vue({
		el: '#incomeRight',
		data: {
			billStateData: [{
					name: '审批通过',
					code: 'PA',
					num: 0
				},
				{
					name: '执行中',
					code: 'EX',
					num: 0
				},
				{
					name: '已到期',
					code: 'FN',
					num: 0
				},
				{
					name: '已完结',
					code: 'EN',
					num: 0
				},
			],
			selectedStateCode: [],
			keyword: '',
			listData: {
				revenueRecognitions: []
			},
			expenditureArr: [],
			expenditureSelected: [],
			revenueRecognitionAmount: 0,
			limitArr: [{
				name: '10',
				code: '10'
			}, {
				name: '15',
				code: '15'
			}, {
				name: '20',
				code: '20'
			}],
			limitSelect: ['10'],
			currentPage: 1,
			totalCount: 0,
			totalPage: 0,
			month: [{
					name: '01',
					code: '01'
				},
				{
					name: '02',
					code: '02'
				},
				{
					name: '03',
					code: '03'
				},
				{
					name: '04',
					code: '04'
				},
				{
					name: '05',
					code: '05'
				},
				{
					name: '06',
					code: '06'
				},
				{
					name: '07',
					code: '07'
				},
				{
					name: '08',
					code: '08'
				},
				{
					name: '09',
					code: '09'
				},
				{
					name: '10',
					code: '10'
				},
				{
					name: '11',
					code: '11'
				},
				{
					name: '12',
					code: '12'
				}
			],
			setyear: [],
			setmonth: [],
			endyear: [],
			endmonth: [],
			rentUnits: [],
			lefthover: 0,
			tophover: 0,
			texthover: false,
			textLong: '',
			projectList: [],
			buildingList: [],
			floorList: [],
			unitList: [],
			projectIds: [],
			projectId: [],
			buildingId: [],
			floorId: [],
			unitId: [],
			isExtenseFlag: false,
			totalRevenueRecognitionAmount: 0,
			permissions: JSON.parse(window.localStorage.getItem('permissions')),
			groupId: null,
			groupOneData: [],
			groupTwoData: [],
			groupThreeData: [],
			selectedGroupData: [],

			isScroll:false,
			localMonth:'',
		},
		computed: {
			yearMonthArr() {
				let yearReg = new Date().getFullYear();
				let yearAdd = new Date().getFullYear();
				let monthReg = new Date().getMonth() + 1;
				let monthAdd = new Date().getMonth() + 1;
				let newArr = [];
				let number = 12;
				for (let i = 0; i <= number; i++) {
					if (monthReg < 1) {
						monthReg = 12;
						yearReg--;
					}
					newArr.unshift(yearReg + '.' + (monthReg < 10 ? '0' + monthReg : monthReg))
					monthReg--;
				}
				for (let i = 0; i < number - 1; i++) {
					if (monthAdd > 11) {
						monthAdd = 0;
						yearAdd++;
					}
					monthAdd++;
					newArr.push(yearAdd + '.' + (monthAdd < 10 ? '0' + monthAdd : monthAdd))
				}
				return newArr;
			},
			yearli() {
				var result;
				var nowYear = new Date().getFullYear();
				result = [{
					name: nowYear - 1,
					code: nowYear - 1
				}, {
					name: nowYear,
					code: nowYear
				}, {
					name: nowYear + 1,
					code: nowYear + 1
				}]
				return result;
			},
			startYearList() {
				var result = []
				var nowYear = new Date().getFullYear();
				var res = nowYear - 15;
				for (var i = 0; i < 100; i++) {
					result.push({
						name: res + i,
						code: res + i
					})
				}
				return result;
			}
		},
		filters: {
			formatNum: function (item) {
				return hlNumberUtil.formatNum(item)
			},
			timestamp: function (time) {
				return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd HH:mm:ss')
			},
			toZero(n) {
				return toZero(n)
			}
		},
		methods: {
			//鼠标经过显示scroll
			showScroll() {
				this.isScroll = true;
			},
			//鼠标离开关闭scroll
			hideScroll() {
				this.isScroll = false;
			},


			selectBillMenu() {
				this.searchSubmit()
			},

			resetTabbar() {
				this.selectedStateCode = []
				this.searchSubmit()
			},
			//选择tab
			handleSelectedAuditState(state) {
				let flag = false,
					index0;
				this.currentPage = 1;

				if (this.selectedStateCode.indexOf('#') >= 0) this.selectedStateCode = []
				this.selectedStateCode.map((item, index) => {
					if (item == state) {
						flag = true;
						index0 = index;
					}
				})
				if (flag) {
					this.selectedStateCode.splice(index0, 1)
					this.getData()
					this.statisticsDt()
					return
				}
				this.selectedStateCode.push(state)
				this.getData()
				this.statisticsDt()
			},
			goToDetail(item) {
				hlUrlUtil.goTo('incomeDetail', {
					ddtab: true,
					id: item.contactId,
					expenditureId: (item.expenditureId || '#')
				}, true)
			},
			//获取费项
			expenditureDt() {
				hlhttp.get({
					url: 'expenditures',
					data: {
						page: 1,
						limit: 20000
					},
					success: (data) => {
						this.expenditureArr = hlArrayUtil.replaceData(data.expenditures, [{
							key: 'id',
							rKey: 'code'
						}]);
					}
				})
			},
			//选择页数
			selectedLimit(arr) {
				this.limitSelect = [arr[0].code];
				this.currentPage = 1;
				this.getData()
			},
			//获取列表																													
			getData() {
				let state = JSON.parse(JSON.stringify(this.selectedStateCode))
				hlhttp.get({
					url: 'revenue_recognitions',
					data: {
						keyword: this.keyword || '#',
						expenditure_name: this.expenditureSelected.length ? this.expenditureSelected[0] : '#',
						contract_audit_state: state.toString() || '#',
						limit: this.limitSelect[0],
						project_id: this.projectId.length ? this.projectId[0] : '#',
						group_id: self.groupId || '#',
						building_id: this.buildingId.length ? this.buildingId[0] : '#',
						floor_id: this.floorId.length ? this.floorId[0] : '#',
						unit_id: this.unitId.length ? this.unitId[0] : '#',
						page: this.currentPage,
						begin_month_period: this.setyear[0] && this.setmonth[0] ? this.setyear[0] + '-' + this.setmonth[0] : '#',
						end_month_period: this.endyear[0] && this.endmonth[0] ? this.endyear[0] + '-' + this.endmonth[0] : '#',
					},
					success: (data) => {
						this.listData = data
						this.totalPage = data.totalPage
						this.totalCount = data.totalCount
					}
				})
			},
			//收入确认统计
			statisticsDt() {
				let state = JSON.parse(JSON.stringify(this.selectedStateCode))

				hlhttp.get({
					url: 'revenue_recognitions/_statistics',
					data: {
						keyword: this.keyword || '#',
						expenditure_name: this.expenditureSelected.length ? this.expenditureSelected[0] : '#',
						begin_month_period: this.setyear[0] && this.setmonth[0] ? this.setyear[0] + '-' + this.setmonth[0] : '#',
						end_month_period: this.endyear[0] && this.endmonth[0] ? this.endyear[0] + '-' + this.endmonth[0] : '#',
						contract_audit_state: state.toString() || '#',
						project_id: this.projectId.length ? this.projectId[0] : '#',
						building_id: this.buildingId.length ? this.buildingId[0] : '#',
						floor_id: this.floorId.length ? this.floorId[0] : '#',
						unit_id: this.unitId.length ? this.unitId[0] : '#',
					},
					success: (data) => {
						this.revenueRecognitionAmount = data
					}
				})
			},
			//收入确认统计 tab count
			statisticsCount() {
				hlhttp.get({
					url: 'revenue_recognitions/_count',
					data: {
						keyword: this.keyword || '#',
						expenditure_name: this.expenditureSelected.length ? this.expenditureSelected[0] : '#',
						contract_audit_state: this.selectedStateCode.length ? this.selectedStateCode.join() : '#',
						project_id: this.projectId.length ? this.projectId[0] : '#',
						building_id: this.buildingId.length ? this.buildingId[0] : '#',
						floor_id: this.floorId.length ? this.floorId[0] : '#',
						unit_id: this.unitId.length ? this.unitId[0] : '#',
						begin_month_period: this.setyear[0] && this.setmonth[0] ? this.setyear[0] + '-' + this.setmonth[0] : '#',
						end_month_period: this.endyear[0] && this.endmonth[0] ? this.endyear[0] + '-' + this.endmonth[0] : '#',
					},
					success: (data) => {
						this.totalRevenueRecognitionAmount = data
						this.billStateData.forEach(el => {
							if (el.code == 'PA') el['num'] = data['totalCountPA']
							if (el.code == 'EX') el['num'] = data['totalCountEX']
							if (el.code == 'FN') el['num'] = data['totalCountFN']
							if (el.code == 'EN') el['num'] = data['totalCountEN']
						})

					}
				})
			},
			//搜索
			searchSubmit() {
				this.currentPage = 1;
				this.getData()
				this.statisticsDt()
				this.statisticsCount()
			},
			//重置
			resetSubmit() {
				this.setyear = [];
				this.setmonth = [];
				this.endyear = [];
				this.endmonth = [];
				this.selectedStateCode = [];
				this.projectId = []
				this.groupId = null
				this.buildingId = []
				this.floorId = []
				this.unitId = []
				this.buildingList = []
				this.floorList = []
				this.unitList = []
				this.keyword = ''
				this.expenditureSelected = []
				this.selectedGroupData = []
				this.limitSelect = ['10']
				this.currentPage = 1

				this.getData()
				this.statisticsDt()
				this.statisticsCount()
			},
			//计算scroll-x
			computeLeft() {
				let yearmonth = new Date().getFullYear() + '.' + toZero(new Date().getMonth() + 1);
				let left = 0;

				this.yearMonthArr.map((item, index) => {
					if (item == yearmonth) {
						left = index;
					}
				})
				this.$refs.overflowLeft.scrollLeft = left * 170;
			},
			//开始日期
			getyearfn(arr) {
				this.setyear = [arr[0].code]
				if (this.setmonth.length) {
					this.statisticsDt()
				}
			},
			getmonthfn(arr) {
				this.setmonth = [arr[0].code]
				if (this.setyear.length) {
					this.statisticsDt()
				}
			},

			//结束日期
			endyearback(arr) {
				this.endyear = [arr[0].code]
				if (this.endmonth.length) {
					this.statisticsDt()
				}
			},
			endmonthback(arr) {
				this.endmonth = [arr[0].code]
				if (this.endyear.length) {
					this.statisticsDt()
				}
			},
			//阻止冒泡
			cancelBubbleTrue() {
				event.cancelBubble = true
				event.stopPropagation()
			},
			//选择费项
			expendSelectFn(arr) {
				this.expenditureSelected = [arr[0].name]
				this.getData()
				this.statisticsDt()
			},
			//导出
			exportExcl() {
				let state = JSON.parse(JSON.stringify(this.selectedStateCode))
				hlhttp.get({
					baseUrl: exclUrl,
					url: 'resource/revenue_recognitions/_export',
					data: {
						keyword: this.keyword || '#',
						expenditure_name: this.expenditureSelected.length ? this.expenditureSelected[0] : '#',
						contract_audit_state: state.toString() || '#',
						project_id: this.projectId.length ? this.projectId[0] : '#',
						building_id: this.buildingId.length ? this.buildingId[0] : '#',
						floor_id: this.floorId.length ? this.floorId[0] : '#',
						unit_id: this.unitId.length ? this.unitId[0] : '#',
						begin_month_period: this.setyear[0] && this.setmonth[0] ? this.setyear[0] + '-' + this.setmonth[0] : '#',
						end_month_period: this.endyear[0] && this.endmonth[0] ? this.endyear[0] + '-' + this.endmonth[0] : '#',
					},
					success: (data) => {
						window.open(data.url);
					}
				})
			},

			openSearch: function () {
				if (this.isExtenseFlag == true) {
					this.isExtenseFlag = false;
					this.projectId = []
					this.buildingId = []
					this.floorId = []
					this.unitId = []
					this.buildingList = []
					this.floorList = []
					this.unitList = []
					return false;
				}
				if (this.isExtenseFlag == false) {
					this.isExtenseFlag = true;
					return false;
				}
			},
			//获取项目
			getProjectData() {
				hlhttp.get({
					url: 'user/projects',
					success: (res) => {
						res = hlArrayUtil.removeRepeat(res, 'projectId')
						this.projectList = hlArrayUtil.replaceData(res, [{
							key: 'projectName',
							rKey: 'name'
						}, {
							key: 'projectId',
							rKey: 'code'
						}]);

					}
				})
			},
			// 获取一级组合数据
			getGroupPrimaryData(data) {
				const self = this
				self.projectId = [data[0].code]
				self.groupId = null
				self.buildingId = []
				self.floorId = []
				self.unitId = []
				self.buildingList = []
				self.floorList = []
				self.unitList = []
				self.selectedGroupData = []
				self.getGroupData().then(data => {
					self.groupOneData = hlArrayUtil.replaceData(data, [{
						key: 'id',
						rKey: 'code'
					}]);
				})

				self.getBuildingData()
			},
			// 选取一级组合数据
			selectOneGroup(res) {
				const self = this
				this.groupId = res['cur']['code']
				this.getGroupData().then(data => {
					self.groupTwoData = hlArrayUtil.replaceData(data, [{
						key: 'id',
						rKey: 'code'
					}])
					self.getBuildingData()
				})
			},
			// 选取二级组合数据
			selectTwoGroup(res) {
				const self = this
				this.groupId = res['cur']['code']
				this.getGroupData().then(data => {
					self.groupThreeData = hlArrayUtil.replaceData(data, [{
						key: 'id',
						rKey: 'code'
					}])
					self.getBuildingData()
				})
			},
			// 选取三级组合数据
			selectThreeGroup(res) {
				this.groupId = res['cur']['code']
			},
			getGroupData() {
				const self = this

				return new Promise(resolve => {
					hlhttp.get({
						url: 'project/grouping_info/_subclass',
						data: {
							'project_id': self.projectId[0],
							'grouping_id': self.groupId || '#'
						},
						success: function (data) {
							resolve(data)
						}
					})
				})
			},
			//获取楼宇
			getBuildingData() {
				const self = this

				hlhttp.get({
					url: 'project/grouping/_building',
					data: {
						project_id: self.projectId.length ? self.projectId[0] : '#',
						grouping_id: self.groupId || '#'
					},
					success: (res) => {
						this.buildingList = hlArrayUtil.replaceData(res, [{
							key: 'buildingId',
							rKey: 'code'
						}]);
					}
				})
			},
			//获取楼层
			getfloorData() {
				hlhttp.get({
					url: 'building/floors',
					data: {
						project_id: this.projectId[0],
						building_id: this.buildingId[0]
					},
					success: (res) => {
						this.floorList = hlArrayUtil.replaceData(res, [{
							key: 'floorName',
							rKey: 'name'
						}, {
							key: 'floorId',
							rKey: 'code'
						}]);
					}
				})
			},
			//获取单元
			getunitData() {
				hlhttp.get({
					url: 'floor/units',
					data: {
						project_id: this.projectId[0],
						building_id: this.buildingId[0],
						floor_id: this.floorId[0]
					},
					success: (res) => {
						this.unitList = hlArrayUtil.replaceData(res, [{
							key: 'unitName',
							rKey: 'name'
						}, {
							key: 'unitId',
							rKey: 'code'
						}]);
					}
				})
			},
			//点击项目获得  id
			getprojectId(arr) {
				this.projectId = [arr[0].code];
				this.getbuildingData()
				this.buildingId = [];
				this.floorId = [];
				this.unitId = [];
			},
			//点击楼宇获得 id
			getbuildingId(arr) {
				this.buildingId = [arr[0].code];
				this.getfloorData()
				this.floorId = [];
				this.unitId = [];
			},
			//点击楼层获得 id
			getfloorId(arr) {
				this.floorId = [arr[0].code];
				this.getunitData()
				this.unitId = [];
			},
			// 点击单元获得 id
			getunitId(arr) {
				this.unitId = [arr[0].code];
			},
		},
		mounted() {
			var timestamp = new Date().getTime()
			this.localMonth = hlDateUtil.formatUTCDate(timestamp, 'yyyy-MM')




			this.expenditureDt()
			this.getData()
			this.statisticsDt()
			// this.computeLeft()
			this.statisticsCount()
			this.getProjectData()
			hlSelectUi()

			document.addEventListener('click', (e) => {
				if (e.srcElement.className.indexOf('timebill') == -1) {
					this.ymflag = false
				}
			}, true)
		}
	})

	function toZero(n) {
		return Number(n) < 10 ? '0' + Number(n) : '' + Number(n)
	}
}())